package com.ro0tsh3ll.controller;

import com.ro0tsh3ll.model.Count;
import com.ro0tsh3ll.service.IdeaKeyService;
import com.ro0tsh3ll.tools.keygen;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Random;

/**
 * Created by Administrator on 2014/6/9.
 */
@Controller
@RequestMapping("/ideaKey")
public class IdeaKeyController {
    @Autowired
    private IdeaKeyService ideaKeyServiceimpl;

    public static final String IDEAKEY_COUNT = "ideaKey";

    @RequestMapping("show")
    public String show(Model model){
        Count count=ideaKeyServiceimpl.findCount("ideaKey");
        model.addAttribute("count",count);
        return "ideaKey";
    }

    @RequestMapping("getRegistrationCode")
    public String getRegistrationCode(String keyName,Model model){
        if (StringUtils.isNotBlank(keyName)){
            Random r = new Random();
            String key= keygen.MakeKey(keyName, 0, r.nextInt(100000));
            model.addAttribute("keyName",keyName);
            model.addAttribute("key",key);
            ideaKeyServiceimpl.upDateCount(IDEAKEY_COUNT);
        }
        return "ideaKey";
    }
}
